import {createRouter,createWebHistory} from 'vue-router'
import {getRoutes} from './service'
const router = createRouter({
    history: createWebHistory(),
    routes:[
        // 使用模块加载，在生产环境（build）可以实现按需加载,提高性能
        {path:'/',name: 'home',component: () => import('@/views/Home.vue'),meta:{title: '首页'}},
        {path:'/:any(.+)',name: 'noFound',component: () => import('@/views/404.vue')},
    ]  
});
// // 添加路由
// router.addRoute({
//     path:'/about',name: 'about',component: () => import('@/views/About.vue')
// });

// router.addRoute({
//     path:'/article',name: 'article',component: () => import('@/views/Article.vue')
// });
// // 移除路由
// router.removeRoute('article');

// 动态添加路由
let isAddRoute = false;
router.beforeEach(async (to,from,next) => {
    const routes = await getRoutes();
    if(!isAddRoute){
        routes.forEach(route =>{
            router.addRoute(route);
        });
        isAddRoute = true;
        return next(to.fullPath);
    }else{
        next()
    }
})
export default router;